package controllers.listeners.lexicon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import models.LexiconModel;
import models.OntologyModel;
import objects.Concept;
import objects.ConceptList;
import objects.Feature;
import objects.FeatureList;
import objects.Form;
import objects.LexiconList;
import objects.Stem;
import views.ViewLexicon;

/* Adds a new lexicon stem */
/* actionPerformed when you click on the "Add Lexicon" button */
public class AddLexiconActionListener implements ActionListener{
	
	ViewLexicon view;
	
	public void setView(ViewLexicon view){
		this.view = view;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		view.removeAllConcepts();
		view.removeAllSenses();
		
		ArrayList<ConceptList> ontologyList = OntologyModel.getOntology();
		ConceptList conceptList = null;
		
		for (ConceptList currentConceptList : ontologyList) {
			/* Check if the selected category is found in the Ontology; therefore, not user-defined */
			if (view.getSelectedCategory().equalsIgnoreCase(currentConceptList.getLabel())) {
				conceptList = currentConceptList;
				break;
			}
		}
		
		/* User-defined */
		if (conceptList == null) {
			for (ConceptList currentConceptList : ontologyList) {
				for (Concept currentConcept : currentConceptList.getConceptList()) {
					view.addConceptToChooser(currentConcept.getName());
				}
			}
		}
		/* Not user-defined */
		else {
			for (Concept currentConcept : conceptList.getConceptList()) {
				view.addConceptToChooser(currentConcept.getName());
			}
		}
		
		JTextField txtLexicon = new JTextField();
		
		Object[] message = {"Enter lexicon value:", txtLexicon,  
							"Select concept value:", view.getCmbConcept(),  
							"Select sense value:", view.getCmbSense(),  
		};
			
		int option = JOptionPane.showConfirmDialog(null, message, "Add Lexicon", JOptionPane.OK_CANCEL_OPTION);  
		
		if (option == JOptionPane.OK_OPTION && txtLexicon != null && view.getCmbConcept() != null && view.getCmbSense() != null) {  
			for (LexiconList currentLexiconList : LexiconModel.getLexicon().getLexiconList()) {
				if (view.getSelectedCategory().equals(currentLexiconList.getLabel())) {
					Stem stem = new Stem();
					stem.setValue(txtLexicon.getText());
					stem.setConcept(view.getSelectedConcept());
					stem.setSense(view.getSelectedSense());
					
					stem.setFormList(assignFormList(currentLexiconList.getFormList()));
					assignDefaultValue(stem, currentLexiconList.getFeatureList());
					
					currentLexiconList.addStemListItem(stem);
					view.refreshTable();
					break;
				}
			}
		}
	}
	
	private ArrayList<Form> assignFormList(ArrayList<String> formList) {
		ArrayList<Form> list = new ArrayList<Form>();
		
		for(int i = 0; i < formList.size(); i++) {
			Form form = new Form();
			form.setName(formList.get(i));
			form.setValue("");
			list.add(form);
		}
		
		return list;
	}
	
	private void assignDefaultValue(Stem stem, ArrayList<FeatureList> featureList) {
		for(int i = 0; i < featureList.size(); i++) {
			Feature item = new Feature();
			item.setName(featureList.get(i).getName());
			item.setValue(featureList.get(i).getValues().get(0));
			stem.addFeatureListItem(item);
		}
	}
	
}
